//
//  ZHScrollView.m
//  ZHScrollView
//
//  Created by Alex on 16/6/22.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ZHScrollView.h"
@interface ZHScrollView ()<UIScrollViewDelegate>
// 滚动视图
@property (nonatomic,strong)UIScrollView *scroll;
// 图片1
@property (nonatomic,strong)UIImageView *leftImage;
// 图片2
@property (nonatomic,strong)UIImageView *rightImage;
// 小圆点
@property (nonatomic,strong)UIPageControl *page;
// 下标
@property (nonatomic,assign)NSInteger index;
// 定时器
@property (nonatomic,strong)NSTimer *timer;
// 存储图片
@property (nonatomic,strong)NSArray *imageArray;
@end


@implementation ZHScrollView

- (UIScrollView *)scroll {
    
    if (!_scroll) {
        _scroll = [[UIScrollView alloc]initWithFrame:self.bounds];
        _scroll.contentSize = CGSizeMake(self.frame.size.width*2, self.frame.size.height);
        _scroll.delegate = self;
        _scroll.pagingEnabled = YES;
        _scroll.bounces = NO;
        _scroll.showsHorizontalScrollIndicator = NO;
    }
    return _scroll;
}

- (UIImageView *)leftImage {
    
    if (!_leftImage) {
        _leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [_leftImage sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    return _leftImage;
}

- (UIImageView *)rightImage {
    if (!_rightImage) {
        _rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        [_rightImage sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
    return _rightImage;
}

- (UIPageControl *)page {
    if (!_page) {
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(150, self.frame.size.height-30, 100, 30)];
        _page.numberOfPages = self.imageArray.count;
    }
    return _page;
}

- (instancetype)initWithFrame:(CGRect)frame  imageArray:(NSArray *)imageArray{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.index = 1;
        //  给数组赋值
        self.imageArray = imageArray;
        
        [self createScrollViewWithFrame:frame];
    }
    return self;
}

- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

// 定时器事件
- (void)timerAction {
    
    [self.scroll setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
}

// scroll 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (self.scroll.contentOffset.x == self.frame.size.width) {
        self.scroll.contentOffset = CGPointZero;
        
        self.page.currentPage = self.index;
        self.index++;
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImage.image = self.rightImage.image;
        [_rightImage sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];

    }
}

// 创建滚动视图
- (void)createScrollViewWithFrame:(CGRect)frame {
    
    [self.scroll addSubview:self.leftImage];
    [self.scroll addSubview:self.rightImage];
    [self addSubview:self.scroll];
    [self addSubview:self.page];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.timer fire];
    });
    
}




@end
